package com.xunlai.infra.permission.business.controller;

import com.xunlai.infra.permission.business.dao.entity.vo.UserMemberVo;
import com.xunlai.infra.permission.business.service.MemberService;
import com.xunlai.infra.permission.business.vo.member.MemberCompleteVo;
import com.xunlai.infra.permission.business.vo.member.MemberInfoVo;
import com.xunlai.infra.permission.business.vo.member.MemberQueryVo;
import com.xunlai.infra.permission.business.vo.member.RechargeVo;
import com.xunlai.infra.permission.common.exception.ExceptionCodeEnum;
import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.common.security.TenantUser;
import com.xunlai.infra.permission.common.vo.BasePageRespVo;
import com.xunlai.infra.permission.common.vo.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @author liang
 * @date 9/3/23 5:00 PM
 */
@RestController
@RequestMapping("/travel/manage/member")
public class MemberController {

    private MemberService memberService;

    @RequestMapping(value = "recharge/{goodsId}",method = {RequestMethod.GET,RequestMethod.POST})
    public BaseResponse<RechargeVo> recharge(@PathVariable Long goodsId){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            try {
                return BaseResponse.success(memberService.recharge(goodsId, Long.valueOf(tenantUser.getId())));
            } catch (PermissionBusinessException e) {
                return BaseResponse.error(e.getCode(),e.getMsg());
            }
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }

    }

    @GetMapping("info")
    public BaseResponse<MemberInfoVo> memberInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            MemberInfoVo info = memberService.getInfo(Long.valueOf(tenantUser.getId()));
            if(info == null) {
                info = new MemberInfoVo();
            }
            info.setAccount(tenantUser.getUsername());
            return BaseResponse.success(info);
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }
    }

    @PostMapping("info/complete")
    public BaseResponse<Void> completeInfo(@RequestBody MemberCompleteVo memberCompleteVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser){
            memberService.completeInfo(Long.valueOf(tenantUser.getId()),memberCompleteVo);
            return BaseResponse.success();
        }else {
            return BaseResponse.error(ExceptionCodeEnum.NOT_LOGIN);
        }
    }

    @GetMapping("list")
    public BaseResponse<BasePageRespVo<UserMemberVo>> list(MemberQueryVo memberQueryVo, @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                           @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        return BaseResponse.success(memberService.listUserMember(memberQueryVo,pageNum,pageSize));
    }


    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }
}
